/**
 * Created by Demon-pro on 2015/6/28.
 */
var Pipeline = function () {

    function init() {
        $.post(basePath + "mill/pipe/getCalculateInit", function () {
        });
        $("#E115").on("change",function(){
            Metronic.blockUI({
                boxed: true,
                message: "数据计算中..."
            });
            $.post(basePath + "mill/pipe/calE116AndE117",function(data) {
                $("#E116").val(data.E116);
                $("#E117").val(data.E117);
                Metronic.unblockUI();
                Pipeline.update("E116");
                Pipeline.update("E117");
            })
        });
        $("#E118,#E119").on("change",function() {
            if (($(this).attr("id") == "E118" && $("#E119").val() != "") || ($(this).attr("id") == "E119" && $("#E118").val() != "")) {
                Metronic.blockUI({
                    boxed: true,
                    message: "数据加载中..."
                });
                calE120();
            }
        });

        var calE120 = function() {
            $.post(basePath + "mill/pipe/calE120",function(data) {
                if (data == null || data == "") {
                    $("#E120").val("不合格");
                } else {
                    $("#E120").val(data);
                    Pipeline.update("E120");
                }
                Metronic.unblockUI();
            })
        }
    }

    return {
        init: function () {
            init();
        },
        update: function (ExParam) {
            var save = function (exParam, exVal) {
                if (validate.isNotNumber(exVal)) {
                    validate.invalid();
                    return;
                }
                $.ajax({
                    url: basePath + "mill/pipe/saveValueOfPipeCalculate",
                    dataType: "json",
                    type: "post",
                    //放了三个值，一个填到默认Ex当中，其他两个分别对应属性值和属性名
                    data: exParam + "=" + exVal + "&paramName=" + exParam + "&paramValue=" + exVal
                });
                validate.valid();
            };
            var $obj = $("#" + ExParam);
            var ExVal = $obj.val();
            validate.init($obj);
            if (ExVal != "") {
                save(ExParam, ExVal);
            } else {
                //判断是否可以计算，如果可以后台计算后返回结果，保存结果，不可以计算提示
                if ($obj.hasClass("calculable")) {
                    $.ajax({
                        url: basePath + "admin/formal/getCalculateValue",
                        dataType: "json",
                        type: "post",
                        data: "paramName=A" + ExParam,
                        success: function (data) {
                            $obj.val(data);
                            save(ExParam, data);
                        }
                    });
                    validate.valid();
                }
                else
                    validate.invalid();
            }
        }
    }
}();
